package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;


@EnableTransactionManagement
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getRroupRelation(Long attrgroupId) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<AttrAttrgroupRelationEntity>().lambda().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId);

        List<Long> collect = relationDao.selectList(wrapper).stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        return this.baseMapper.selectBatchIds(collect);
    }
    @Transactional
    @Override
    public void saveAttr(AttrVo attrVo) {
        AttrEntity entity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,entity);
        this.save(entity);
        //2、保存关联关系
        // if (attrVo.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() && attrVo.getAttrGroupId() != null) {
        //     AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        //     relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
        //     relationEntity.setAttrId(attrEntity.getAttrId());
        //     relationEntity.setAttrSort(0);
        //     relationDao.insert(relationEntity);
        // }
    }

    /**
     * 获取当前分组没有关联的所有属性
     *
     * @param attrgroupId attrgroupId
     * @param params params
     * @return PageUtils
     */
    @Override
    public PageUtils getNoReleationAttr(Long attrgroupId, Map<String, Object> params) {
        //1、当前分组只能关联自己所属分类里面的所有属性
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
        //2、当前分组只能关联别的分组没有引用的属性
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>()
                                        .lambda()
                                        .eq(AttrGroupEntity::getCatelogId, catelogId)
                                        .ne(AttrGroupEntity::getAttrGroupId, attrgroupId);
        //2.1查出当前分类下的 其他分组 的属性id集合
        List<AttrGroupEntity> otherGroupList = attrGroupDao.selectList(wrapper);
        List<Long> otherAttrIdList = otherGroupList.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
        //2.2这些分组关联的属性
        List<AttrAttrgroupRelationEntity> otherGroupRelationLIst = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                                                                    .lambda()
                                                                    .in(AttrAttrgroupRelationEntity::getAttrGroupId, otherAttrIdList));
        List<Long> needAttrIdList = otherGroupRelationLIst.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        LambdaQueryWrapper<AttrEntity> attrEntityLambdaQueryWrapper = new QueryWrapper<AttrEntity>().lambda()
                                                .eq(AttrEntity::getCatelogId, catelogId)
                                                .notIn(AttrEntity::getAttrId, needAttrIdList);
        // 处理参数模糊插叙的情况
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            attrEntityLambdaQueryWrapper.and((w)->w.eq(AttrEntity::getAttrId,key).or().like(AttrEntity::getAttrName,key));
        }
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), attrEntityLambdaQueryWrapper);

        return new PageUtils(page);
    }
}